package by.vkhalko.asamo.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.vkhalko.asamo.dataaccess.CompanyDao;
import by.vkhalko.asamo.datamodel.Company;
import by.vkhalko.asamo.services.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(CompanyServiceImpl.class);

	@Inject
	private CompanyDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of CompanyService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Company get(Long id) {
		Company entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Company company) {
		if (company.getId() == null) {
			LOGGER.debug("Save new: {}", company);
			dao.insert(company);
		} else {
			LOGGER.debug("Update: {}", company);
			dao.update(company);
		}
	}

	@Override
	public void delete(Company company) {
		LOGGER.debug("Remove: {}", company);
		dao.delete(company.getId());
	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all companies");
		dao.deleteAll();
	}

	@Override
	public List<Company> getAllCompanies() {
		return dao.getAllCompanies();

	}

	@Override
	public List<Company> getAllCompaniesByName(String name) {
		return dao.getAllCompaniesByName(name);
	}

}
